<?php

/**
 * IndexController - The default controller class
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class Administrator_BulletinController extends Zend_Controller_Action 
{
	

	
	public function init(){
		
		$member=new CMember();
		
		if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())){	
			
			$this->_redirect(MY_URL.'/administrator-auth-login');
			
		}
		
		$memberArray = $member->getUserDetails(array('uid' => $_COOKIE[COOKIE_PRE.'uid']));
    	if($_COOKIE[COOKIE_PRE.'uid']=='1'){
				
		} else {
        	$strarray = explode(",", $memberArray[0]['menuflag']);
        	
        	if (!in_array('8', $strarray)) {
        		exit('no priviage');
        	}
		}
		
	}
	
	public function preDispatch()
	{
		
		$this->getHelper ('layout')->setLayout('framelayout');
		
		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');
		
		//$this->getHelper('User')->getinfo($this->view,$this->_request->getModuleName(),$this->_request->getControllerName());
		
	}

	
    public function indexAction() 
    {

    	$page=$this->_request->getParam('page');
    	
		$item=new CItem();
		
		$itemCounts=$item->getAllItemCounts('bulletin');
		
    	Zend_Loader::loadClass("CFilter");
    	
    	$pageArray = CFilter::filterPage ( $page, $itemCounts, 5 );
		
		$itemArray=$item->getAllItems($pageArray['from'],5,'bulletin');
		
		Zend_Loader::loadClass("CPage");
    	
    	$pageString = CPage::divPages( $pageArray ['pageNumber'], $pageArray ['page'], ROOT_PATH."/administrator-bulletin-index?"); //生成分页字符串
    	
    	$this->view->assign ( "pageString", $pageString );
    	
    	$this->view->assign ( "pageArray", $pageArray );
		
		$this->view->assign('itemArray',$itemArray);

    }
    
    public function addAction(){
    	
		require_once (MY_ROOT."/fckeditor/fckeditor.php");
		
		$oFCKeditor = new FCKeditor('FCKeditor1') ;
		
		$oFCKeditor->BasePath	= ROOT_PATH.'/fckeditor/' ;
		
		$oFCKeditor->Value		= '&nbsp';
		
		$oFCKeditor->Height=400;
		
		$oFCKeditor->Width="98%";
		
		$this->view->assign('oFCKeditor',$oFCKeditor);
    	
    }
    
    public function doaddAction(){
    	
    	$this->_helper->viewRenderer->setNoRender();
    	
        if(!$this->getRequest()->isPost()){
			
			$this->view->showMsg("发生未知错误!");
			
			return false;
			
		}
    	
    	$thisArray=$this->_request->getParams();
    	
		$filters = array(
    		'*'   => 'StringTrim'
		);

		$validators = array(
		);
		
		$input = new Zend_Filter_Input($filters, $validators, $thisArray);
    	
		/*插入到条目表 start*/
		$item=new CItem();
		$itemData=array(
						'uid'=>$_COOKIE[COOKIE_PRE.'uid'],
						'flid'=>0,
						'subject'=>$input->subject,
						'item_type'=>'bulletin',
						'add_time'=>time(),
						'update_time'=>time(),
						'is_banned'=>'0',
				  );
		$itemid=$item->createItem($itemData);
    	/*插入到条目表 end*/
		
		
		/*图片附件 start*/
		if(count($thisArray['imgUrl'])>0){
			$resultArray=$this->addAttachment($input,$itemid);
			$attachmentData=$resultArray[0];
			$attachment=new CAttachment();
			$attachment->createAttachments($attachmentData);
		}
		/*图片附件 end*/
		
		$content=htmlspecialchars_decode($input->FCKeditor1);
		
		$content=str_replace('html/upload/temp_img/','html/upload/item_img/',$content);
		
    	/*插入到公告表 start*/
		$bulletin=new CBulletin();
		$data=array(
						'itemid'=>$itemid,
						'valid_days'=>$input->valid_days,
						'content'=>$content,
					 );	
		$bulletin->createBulletin($data);
		/*插入到公告表 end*/
		
		$this->view->showMsg("发表成功!",'administrator-bulletin-index');
		
		return true;
    	
    }
    
    public function addAttachment($input,$itemid){
    	
    	$attachmentData=array();
		
		$path=new CPath();
		
		$image=new CImage();
		
		Zend_Loader::loadClass('CFile');
		
		for($i=0;$i<count($input->imgUrl);$i++){
			
			$newPath=$path->toDbPath('item',$input->imgUrl[$i]);//生成新的item图片路径
			
			CFile::copyAndUnlink($path->toAbsoluteRootPath($input->imgUrl[$i]),$path->toAbsoluteRootPath($newPath));//从临时文件夹转到item图片文件夹
			
			$cutPath=$image->cutPhoto($newPath, '200', $path->getPathFromType('cut_item'));//截图
			
			$attachmentData[$i]['itemid']=$itemid;
			
			$attachmentData[$i]['attachment_url']=$newPath;
			
			$attachmentData[$i]['cut_url']=$path->toDbPath('cut_item',$cutPath);
			
			$attachmentData[$i]['intro']=$input->imgIntro[$i];
			
		}
		
		return array($attachmentData);
    	
    }
    
    public function editAction(){
    	
	$itemid=$this->_request->getParam('itemid');

    	$item=new CItem();
    	
    	$itemArray=$item->getDetail($itemid,'bulletin',false);
    	
    	$this->view->assign('itemArray',$itemArray);
    	
    	$attachment=new CAttachment();
    	
    	$attachmentArray=$attachment->getAttachments($itemid);
    	
    	$this->view->assign('attachmentArray',$attachmentArray);
    	
		require_once (MY_ROOT."/fckeditor/fckeditor.php");
		
		$oFCKeditor = new FCKeditor('FCKeditor1') ;
		
		$oFCKeditor->BasePath	= ROOT_PATH.'/fckeditor/' ;
		
		$oFCKeditor->Value		= $itemArray[0]['content'];
		
		$oFCKeditor->Height=400;
		
		$oFCKeditor->Width="98%";
		
		$this->view->assign('oFCKeditor',$oFCKeditor);	
    	
    }
    
    public function doeditAction(){
    	
    	$this->_helper->viewRenderer->setNoRender();
    	
    	if(!$this->getRequest()->isPost()){
			
			$this->view->showMsg("发生未知错误!");
			
			return false;
			
		}
		
		$paramArray=$this->_request->getParams();
		
		$itemid=$paramArray['itemid'];
		/*
		Zend_Session::start();
		
    	if(strtolower($paramArray['productyz'])!=strtolower($_SESSION['adminsProductConfirmCode'])){
			
			$this->view->showMsg("验证码错误!");
			
			return false;
			
		}*/
		
		
		$filters = array(
    		'*'   => 'StringTrim'
		);

		$validators = array(
		);
		
		$input = new Zend_Filter_Input($filters, $validators, $paramArray);	
		
		/*修改条目表 start*/
		$item=new CItem();
		$itemData=array(
						'flid'=>0,
						'subject'=>$input->subject,
						'update_time'=>time(),
				  );
		$item->editItem($itemid,$itemData);
		/*修改条目表 end*/

		/*图片附件 start*/
		$resultArray=$this->editAttachment($input,$itemid);
		$attachmentData=$resultArray[0];
		$attachment=new CAttachment();
		$attachment->editAttachments($attachmentData);
		/*图片附件 end*/
		
		$content=htmlspecialchars_decode($input->FCKeditor1);
		
		$content=str_replace('html/upload/temp_img/','html/upload/item_img/',$content);
		
    	/*修改公告表 start*/
		$bulletin=new CBulletin();
		$data=array(
						'valid_days'=>$input->valid_days,
						'content'=>$content,
					 );	
		$bulletin->editBulletin($itemid,$data);
		/*修改公告表 end*/

		$this->view->showMsg("修改成功!",'administrator-bulletin-index');

		return true;
    	
    }
    
    public function editAttachment($input,$itemid){
    	
    	$attachmentData=array();
		
		$path=new CPath();
		
		$image=new CImage();
		
		Zend_Loader::loadClass('CFile');
		
		$cut_url='';
		
		for($i=0;$i<count($input->imgUrl);$i++){
			
			$attachmentData[$i]['attachmentid']=$input->attachmentid[$i];
			
			if($attachmentData[$i]['attachmentid']==''){
				
				$newPath=$path->toDbPath('item',$input->imgUrl[$i]);//生成新的item图片路径
				
				CFile::copyAndUnlink($path->toAbsoluteRootPath($input->imgUrl[$i]),$path->toAbsoluteRootPath($newPath));//从临时文件夹转到item图片文件夹
				
				$cutPath=$image->cutPhoto($newPath, '200', $path->getPathFromType('cut_item'));//截图	

				$cut_url=$path->toDbPath('cut_item',$cutPath);
				
				$attachmentData[$i]['attachment_url']=$newPath;
			
				$attachmentData[$i]['cut_url']=$cut_url;
				
				$attachmentData[$i]['itemid']=$itemid;
				
			}
			
			$attachmentData[$i]['intro']=$input->imgIntro[$i];


			
		}
		
		return array($attachmentData);
    	
    }
	

    
    public function uploadimgAction(){
    	
    	header("Content-type: text/html; charset=utf-8");
    	
    	$this->getHelper ('layout')->disableLayout ();
    	  	
    	$upload=new CUpload();
    	
    	$path=new CPath();
    	
    	$thisPath=$path->getPathFromType('temp');
	
    	$imageArray=$upload->uploadFile($thisPath,null,null,'500000');
    	
    	$isSuccess=false;
    	
    	if(is_array($imageArray)){
    		
    		$isSuccess=true;
    		
	    	$imgUrl=$path->toDbPath('temp',$imageArray[0]);
	    	
	    	$this->view->assign('imgUrl',$imgUrl);
	    	
    	}else{
    		
    		$error=$imageArray;
    		
    		$this->view->assign('error',$error);
    		
    	}
    	
    	$this->view->assign('isSuccess',$isSuccess);	
    	
    }
    
    public function delimgAction(){
    	
    	$this->getHelper ('layout')->disableLayout ();
    	
    	$this->_helper->viewRenderer->setNoRender();
    	
    	if(unlink(MY_ROOT.'/'.$this->_request->getParam('imgurl'))){
    		
    		echo 'success';
    		
    	}else{
    		
    		echo 'error';
    		
    	}
    	
    }
    
    public function delimgbyidAction(){
    	
    	$this->_helper->viewRenderer->setNoRender();
		
		$this->getHelper ('layout')->disableLayout ();
    	
    	$attachmentid=$this->_request->getParam('attachmentid');
    	
    	$is_cut_url=$this->_request->getParam('is_cut_url');//当is_cut_url不为1即表明用户要删除的图片是截图
    	
    	$attachment=new CAttachment();

    	$attachmentArray=$attachment->getAttachmentByAttachmentid($attachmentid);
    	
    	$attachment_url=$attachmentArray['attachment_url'];
    	
    	$cut_url=$attachmentArray['cut_url'];
    	
    	$itemid=$attachmentArray['itemid'];
    	
    	$path=new CPath();
    	
    	@unlink($path->toAbsoluteRootPath($attachment_url));
    	
    	@unlink($path->toAbsoluteRootPath($cut_url));
    	
    	if($is_cut_url!=''){
    		
			$product=new CProduct();
			
			$product->editProduct($itemid,array('cut_url'=>''));
    		
    	}
    	
    	$attachment->delAttachmentsById(array($attachmentid));

    	echo 'success';

    	
    }
    
    public function dodelAction(){
    	
    	$this->_helper->viewRenderer->setNoRender();
    	
    	$itemid=$this->_request->getParam('itemid');
    	
    	if($itemid==''){
    		
    		$itemidArray=$this->_request->getParam('itemArray');
    		
    	}else{
    		
    		$itemidArray=array($itemid);
    		
    	}
    	
    	
    	$bulletin=new CBulletin('seller');
    	
    	$item=new CItem();
    	
    	$bulletin->delBulletins($itemidArray);
    	
    	$item->delItems($itemidArray);
    	
    	$attachment=new CAttachment();
    	
    	$attachment->delAttachmentsByItemid($itemidArray);
    	
    	$this->view->showMsg('删除成功!');
    	
    }
    
	public function postAction(){
	
		//$this->_helper->viewRenderer->setNoRender();
		
		//$this->getHelper ('layout')->disableLayout ();
	
	}
    

    
}
